package com.talos.ums.controller.admin.roleuser;

import com.talos.framework.apilog.core.annotation.ApiAccessLog;
import com.talos.framework.common.pojo.CommonResult;
import com.talos.framework.common.pojo.PageParam;
import com.talos.framework.common.pojo.PageResult;
import com.talos.framework.common.util.object.BeanUtils;
import com.talos.framework.excel.core.util.ExcelUtils;
import com.talos.ums.controller.admin.roleuser.vo.UmsRoleUserPageReqVO;
import com.talos.ums.controller.admin.roleuser.vo.UmsRoleUserRespVO;
import com.talos.ums.controller.admin.roleuser.vo.UmsRoleUserSaveReqVO;
import com.talos.ums.dal.dataobject.roleuser.UmsRoleUserDO;
import com.talos.ums.service.roleuser.UmsRoleUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.talos.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.talos.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 角色用户关系表")
@RestController
@RequestMapping("/ums/role-user")
@Validated
public class UmsRoleUserController {

    @Resource
    private UmsRoleUserService roleUserService;

    @PostMapping("/create")
    @Operation(summary = "创建角色用户关系表")
    @PreAuthorize("@ss.hasPermission('ums:role-user:create')")
    public CommonResult<Long> createRoleUser(@Valid @RequestBody UmsRoleUserSaveReqVO createReqVO) {
        return success(roleUserService.createRoleUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新角色用户关系表")
    @PreAuthorize("@ss.hasPermission('ums:role-user:update')")
    public CommonResult<Boolean> updateRoleUser(@Valid @RequestBody UmsRoleUserSaveReqVO updateReqVO) {
        roleUserService.updateRoleUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除角色用户关系表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ums:role-user:delete')")
    public CommonResult<Boolean> deleteRoleUser(@RequestParam("id") Long id) {
        roleUserService.deleteRoleUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得角色用户关系表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ums:role-user:query')")
    public CommonResult<UmsRoleUserRespVO> getRoleUser(@RequestParam("id") Long id) {
        UmsRoleUserDO roleUser = roleUserService.getRoleUser(id);
        return success(BeanUtils.toBean(roleUser, UmsRoleUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得角色用户关系表分页")
    @PreAuthorize("@ss.hasPermission('ums:role-user:query')")
    public CommonResult<PageResult<UmsRoleUserRespVO>> getRoleUserPage(@Valid UmsRoleUserPageReqVO pageReqVO) {
        PageResult<UmsRoleUserDO> pageResult = roleUserService.getRoleUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UmsRoleUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出角色用户关系表 Excel")
    @PreAuthorize("@ss.hasPermission('ums:role-user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRoleUserExcel(@Valid UmsRoleUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UmsRoleUserDO> list = roleUserService.getRoleUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "角色用户关系表.xls", "数据", UmsRoleUserRespVO.class,
                        BeanUtils.toBean(list, UmsRoleUserRespVO.class));
    }

}
